﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.guide.actions {
    import Engine.Classes.*;
    import Engine.Managers.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import com.greensock.*;
    import GameMode.*;
    import Modules.guide.*;

    public class GAPanMap extends GuideAction {

        public static const MODE_TILE:int = 0;
        public static const MODE_TARGET_CLASS:int = 1;
        public static const MODE_TARGET_TYPE:int = 2;
        public static const MODE_ACTIVE_ROLL_CALL_TYPE:int = 3;

        public static var SCROLL_PER_SECOND:Number = 200;
        public static var MAX_SCROLL_TIME:Number = 2;
        public static var MIN_SCROLL_TIME:Number = 0.4;
        public static var isPanningMap:Boolean = false;

        protected var m_mode:int;
        protected var m_x:int = 0;
        protected var m_y:int = 0;
        protected var m_offsetX:int = 0;
        protected var m_offsetY:int = 0;
        protected var m_targetClass:Class;
        protected var m_targetObjectNames:Array;

        public function GAPanMap(){
            this.m_targetObjectNames = [];
            super();
        }
        override public function createFromXml(_arg1:XML):Boolean{
            var _local10:Object;
            var _local2:XML = checkAndGetElement(_arg1, "location")[0];
            if (!_local2){
                return (false);
            };
            var _local3:String = _local2.@mode;
            if (_local3.length > 0){
                _local3 = _local3.toLowerCase();
                switch (_local3){
                    case "tile":
                        this.m_mode = MODE_TILE;
                        break;
                    case "targetclass":
                        this.m_mode = MODE_TARGET_CLASS;
                        break;
                    case "targettype":
                        this.m_mode = MODE_TARGET_TYPE;
                        break;
                    case "activerollcalltype":
                        this.m_mode = MODE_ACTIVE_ROLL_CALL_TYPE;
                        break;
                    default:
                        return (false);
                };
            } else {
                this.m_mode = MODE_TILE;
            };
            var _local4:String = _local2.@className;
            if (_local4.length > 0){
                _local10 = getDefinitionByName(("Classes." + _local4));
                if (_local10 == null){
                    return (false);
                };
                this.m_targetClass = (_local10 as Class);
            };
            var _local5:String = _local2.@typeName;
            if (_local5.length > 0){
                this.m_targetObjectNames = _local5.split(",");
            };
            var _local6:String = _local2.@x;
            var _local7:String = _local2.@y;
            if ((((_local6.length > 0)) && ((_local7.length > 0)))){
                this.m_x = int(_local6);
                this.m_y = int(_local7);
            };
            var _local8:String = _local2.@offsetX;
            if (_local8.length > 0){
                this.m_offsetX = int(_local8);
            };
            var _local9:String = _local2.@offsetY;
            if (_local9.length > 0){
                this.m_offsetY = int(_local9);
            };
            if ((((this.m_mode == MODE_TILE)) && (!((((_local6.length > 0)) && ((_local7.length > 0))))))){
                ErrorManager.addError("Tile or default mode requires 'x' and 'y' attributes!");
                return (false);
            };
            if ((((this.m_mode == MODE_TARGET_CLASS)) && ((this.m_targetClass == null)))){
                ErrorManager.addError("Target Class mode requires a 'className' attribute!");
                return (false);
            };
            return (true);
        }
        override public function update(_arg1:Number):void{
            var _local2:GameObject;
            var _local3:Vector3;
            super.update(_arg1);
            switch (this.m_mode){
                case MODE_TILE:
                default:
                    this.panTo(this.m_x, this.m_y, this.m_offsetX, this.m_offsetY);
                    break;
                case MODE_TARGET_CLASS:
                    _local2 = GuideUtils.getGameObjectByClassName(this.m_targetClass);
                    if (!_local2){
                        break;
                    };
                    _local3 = _local2.getPosition();
                    this.panTo(_local3.x, _local3.y, this.m_offsetX, this.m_offsetY);
                    break;
                case MODE_TARGET_TYPE:
                    _local2 = GuideUtils.getGameObjectByTypeName(this.m_targetObjectNames[0]);
                    if (!_local2){
                        break;
                    };
                    _local3 = _local2.getPosition();
                    this.panTo(_local3.x, _local3.y, this.m_offsetX, this.m_offsetY);
                    break;
                case MODE_ACTIVE_ROLL_CALL_TYPE:
                    _local2 = GuideUtils.getActiveRollCallByTypeNames(this.m_targetObjectNames);
                    if (!_local2){
                        break;
                    };
                    _local3 = _local2.getPosition();
                    this.panTo(_local3.x, _local3.y, this.m_offsetX, this.m_offsetY);
            };
            removeState(this);
        }
        protected function panTo(_arg1:int, _arg2:int, _arg3:int, _arg4:int):void{
            var _local5:Vector2 = GlobalEngine.viewport.getScrollPosition();
            var _local6:Object = {
                startPos:_local5,
                alpha:0
            };
            var _local7:IsoViewport = (GlobalEngine.viewport as IsoViewport);
            _local7.centerOnTilePos((_arg1 + _arg3), (_arg2 + _arg4));
            _local6.endPos = _local7.getScrollPosition();
            _local7.setScrollPosition(_local6.startPos);
            var _local8:Number = _local5.subtract(_local6.endPos).length;
            var _local9:Number = (_local8 / SCROLL_PER_SECOND);
            _local9 = Math.max(_local9, MIN_SCROLL_TIME);
            _local9 = Math.min(_local9, MAX_SCROLL_TIME);
            isPanningMap = true;
            TweenLite.to(_local6, _local9, {
                alpha:1,
                onUpdate:this.onUpdateScrollTween,
                onUpdateParams:[_local6],
                onComplete:this.finishedPanning
            });
        }
        protected function onUpdateScrollTween(_arg1:Object):void{
            var _local2:Vector2 = Vector2.lerp(_arg1.startPos, _arg1.endPos, _arg1.alpha);
            _local2 = GameMode.limitBackgroundScrolling(_local2);
            GlobalEngine.viewport.setScrollPosition(_local2);
            Global.world.updateWorldObjectCulling(GameWorld.CULL_CHECK_ALL);
            if ((Global.world.getTopGameMode() is GMObjectMove)){
                (Global.world.getTopGameMode() as GMObjectMove).onMouseMove(null);
            };
        }
        protected function finishedPanning():void{
            isPanningMap = false;
        }

    }
}//package Modules.guide.actions 
